package obecto.list
{
	import flash.utils.Dictionary;
	
	public class CheckList
	{	
		public function get isComplete() : Boolean
		{
			var result : Boolean = true;
			for (var item : * in items)
			{
				var checked : Boolean = Boolean(items[item]);
				if (!checked)
				{
					result = false;
					break;
				}
			}
			return result;
		}
		
		private var items : Dictionary;
		
		public function CheckList()
		{
			items = new Dictionary(true);
		}
		
		public function addItem(item : Object) : void 
		{
			assertItemLack(item);
			
			items[item] = false;
		}
		
		public function removeItem(item : Object) : void
		{
			assertItemExistence(item)
			
			delete items[item];
		}
		
		public function checkItem(item : Object) : void 
		{
			assertItemExistence(item);
			
			items[item] = true;
		}
		
		public function uncheckItem(item : Object) : void 
		{
			assertItemExistence(item);
			
			items[item] = false;
		}
		
		public function reset() : void 
		{
			for (var item : * in items)
			{
				items[item] = false;
			}
		}
		
		private function assertItemLack(item : Object) : void 
		{
			if (items[item] != null)
			{
				throw new Error("Item is already in the list: " + item);
			}
		}
		
		private function assertItemExistence(item : Object) : void 
		{
			if (items[item] == null)
			{
				throw new Error("No such item in the list: " + item);
			}
		}

	}
}